<?php 

/**
 * IAcltree File
 * 
 *
 * PHP Version: 5.3.26
 * 
 * @category Acltree
 * @package  Acltree_Package
 * @author Johari <joharijumali@gmail.com>  
 */

namespace Admin\Libraries;

use Admin\Models\Modul\Page;
use Admin\Models\User\Role;
use Admin\Models\User\Acl;
use Laravel\Str;
use Bootstrapper\Navigation;
use Bootstrapper\Tabbable;
use Bootstrapper\Form;

/**
 * Acltree class
 * 
 * Class to generate ACL tree view
 *
 * @category Acltree
 * @package  Acltree_Package
 * @author Johari <joharijumali@gmail.com>   
 */

class Acltree
{   

     /**
	  * datasource Method
	  *
	  * Gathering tree datasource
	  * 
	  * @return void
	  */	

	protected static function datasource(){

		$page = Page::all();

		$dataMassage = array();

		foreach($page as $content){

			if(isset($content->smp_action) && $content->smp_action_alias != '' ){
				$dataMassage[$content->smp_modul][$content->smp_controller][$content->smp_action] = $content->smp_action_alias ;
			}
		}

		return $dataMassage;
	}


     /**
	  * render Method
	  *
	  * Rendering ACL tree view
	  * 
	  * @return void
	  */

	public static function render(){

		$rolelist = Role::all();
		$page = Acltree::datasource();

		$acl = Acl::allAcl();

		$content = array();
		$fot = 1;
		foreach ($rolelist as $role){
			$subcontent = '<div class="row-fluid"><div class="span12">';
			$subArray = array();
			$subfot = 1;			
			$checked = '';
			
			foreach ($page as $modul => $selection){

				$subpage =  '';	
				foreach ($selection as $controller => $action){
					

					// $subpage .= '<div class="row-fluid"><blockquote class="pull-left">'.Str::upper($controller).'</blockquote></div >';
					$subpage .= '<div class="well well-small">';
					foreach ($action as $key => $value) {
						$subpage .= '<span style="padding-right:5px;width:auto;">';

						if(in_array($role->sur_id,array_keys($acl)) && in_array($modul,array_keys($acl[$role->sur_id])) &&  in_array($controller,array_keys($acl[$role->sur_id][$modul])) &&  in_array($key,array_keys($acl[$role->sur_id][$modul][$controller])) && $acl[$role->sur_id][$modul][$controller][$key] == 1){
							$subpage .= Form::inline_labelled_checkbox('acl['.$role->sur_id.']['.$modul.']['.$controller.']['.$key.']', Str::title($value), null, array('checked'=>true,'style'=>'padding:2px'));
						}else{
							$subpage .= Form::inline_labelled_checkbox('acl['.$role->sur_id.']['.$modul.']['.$controller.']['.$key.']', Str::title($value));
						}

						$subpage .= '</span>';
					}

					$subpage .= '</div >';
					
				}
				
				$subpage .=  ''; 

				$subactive = ($subfot == 1)? true:false;
				$subfot ++;

				array_push($subArray,array(Str::upper($modul),$subpage,$subactive));
			}

			$nav = Navigation::links($subArray);
			$subcontent .= Tabbable::tabs($nav);
			$subcontent .=  '</div ></div >'; 

			$active = ($fot == 1)? true:false;
			$fot ++;

			array_push($content,array(Str::upper($role->sur_name),$subcontent,$active));
		}
		
		$nav = Navigation::links($content);
		$tab = Tabbable::tabs_left($nav);

		return $tab;

	}


}

?>
